package com.dragon.refect.clazz;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.Arrays;

/**
 *
 * constructor.setAccessible(boolean flag)
 *
 * 将此对象的可访问标志设置为指示的布尔值。
 *
 * 值为true表示反射的对象在使用时应禁止Java语言访问检查。
 * 值为false表示所反射的对象应强制执行Java语言访问检查。
 *
 * @author dragon
 * @since 2019/12/1
 */
public class ClassGetDeclaredConstructorTest {

    public static void main(String[] args) throws NoSuchMethodException, IllegalAccessException,
            InvocationTargetException, InstantiationException {
        Class<Person> personClass = Person.class;
        Constructor<?>[] declaredConstructors = personClass.getDeclaredConstructors();
        System.out.println(Arrays.toString(declaredConstructors));

        Constructor<Person> declaredConstructor = personClass.getDeclaredConstructor(String.class, Integer.class);
        System.out.println(declaredConstructor.newInstance("dragon", 198));

        Constructor<Person> declaredConstructor1 = personClass.getDeclaredConstructor(String.class);
        declaredConstructor1.setAccessible(true);
        System.out.println(declaredConstructor1.newInstance("dragon"));
    }
}
